﻿namespace NEclipse.CoreLib.Workspace
{
  using System.Collections.Generic;
  using System.IO;
using System.Xml.Serialization;

  /// <summary>
  /// Class which manages the whole workspace.
  /// </summary>
  public class WorkspaceManager
  {
    /// <summary>
    /// Name of the workspace configuration file.
    /// </summary>
    private const string WorkspaceConfigFileName = "WorkspaceConfig.xml";

    private WorkspaceManagerSettings settings;

    /// <summary>
    /// List of all projects in the current workspace.
    /// </summary>
    private List<IProject> projects = new List<IProject>();

    /// <summary>
    /// Initializes a new instance of the <see cref="WorkspaceManager"/> class.
    /// </summary>
    /// <param name="workspaceDirectory">The workspace directory.</param>
    public WorkspaceManager(string workspaceDirectory)
    {
      this.WorkspaceDirectory = workspaceDirectory;
      if (File.Exists(this.WorkspaceDirectory))
      {
        XmlSerializer ser = new XmlSerializer(typeof(WorkspaceManagerSettings));
        settings = (WorkspaceManagerSettings)ser.Deserialize(new StreamReader(this.WorkspaceDirectory));
      }
      else
      {
        settings = new WorkspaceManagerSettings();
      }
    }

    /// <summary>
    /// Gets the workspace directory.
    /// </summary>
    public string WorkspaceDirectory { get; private set; }

    /// <summary>
    /// Gets the workspace config file.
    /// </summary>
    private string WorkspaceConfigFile
    {
      get
      {
        return Path.Combine(this.WorkspaceDirectory, WorkspaceConfigFileName);
      }
    }

    /// <summary>
    /// Gets the projects of the current workspace.
    /// </summary>
    public IEnumerable<IProject> Projects
    {
      get
      {
        return this.projects;
      }
    }

    public void SaveConfiguration()
    {
      XmlSerializer ser = new XmlSerializer(typeof(WorkspaceManagerSettings));
      ser.Serialize(new StreamWriter(this.WorkspaceConfigFile), this.settings);
    }
  }
}
